import Vue from 'vue';
import Router from 'vue-router';
import Main from '@/views/Main';
import Login from '@/views/base/login/Login';
import Home from '@/views/base/home/Home';
import UpdatePassword from '@/views/base/login/UpdatePassword';
import Auth from '@/views/base/auth/Auth';
import Organization from '@/views/base/organization/Organization';
import Role from '@/views/base/role/Role';
import Bank from '@/views/base/bank/Bank';
import Feedback from '@/views/base/feedback/Feedback';
import User from '@/views/base/user/User';
import Function from "@/views/base/function/Function";
import Dict from "@/views/base/dict/Dict";
import QuizRecord from "@/views/quizRecord/QuizRecord";
import Course from "@/views/course/Course";
import Dashboard from "@/views/dashboard/Dashboard";
import DictItem from '@/views/base/dict/DictItem';
Vue.use(Router);

//添加以下代码
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
  mode: 'hash',
  routes: [
    {
      path: '/main',
      name: 'Main',
      component: Main,
      meta: {
        title: 'PK 平台',
        requireAuth: true // 标识该路由是否需要登录
      },
      children: [
        {path: '/home', component: Home,meta: {title: '数据看板'}},
        {path: '/auth', component: Auth,meta: {title: '权限管理'}},
        {path: '/organization', component: Organization},
        {path: '/role', component: Role},
        {path: '/user', component: User},
        {path: '/bank', component: Bank},
        {path: '/function', component: Function},
        {path: '/dict', component: Dict},
        {path: '/dictItem', component: DictItem},
        {path: '/quizRecord', component: QuizRecord},
        {path: '/course', component: Course},
        {path: '/feedback', component: Feedback },
        {path: '/dashboard', component: Dashboard },
      ]
    },
    {
      path: '/',
      name: 'Login',
      component: Login,
      meta: {
        title: '在线pk平台'
      }
    },
    {
      path: '/updatePassword',
      name: 'UpdatePassword',
      component: UpdatePassword,
      meta: {
        title: '修改密码'
      }
    },
  ],
});

router.beforeEach((to, from, next) => {
//如果去往登录页则放行
  if (to.path === '/'||to.path === '/updatePassword'||to.path === '/bigData') {
    next();
  } else {
    // 从本地存储里获取token
    let token = localStorage.getItem('token');
    // 判断token是否为空如果为空则跳转到登录页 如果有则放行
    if (token === null || token === '') {
      next({path:'/'});
    } else {
      next();
    }
  }
});
export default router
